package com.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.demo.view.LoadingDialog;
import com.module.commonui.R;
import com.tg.appcommon.android.TGApplicationBase;

public abstract class BaseFragment extends Fragment {

    protected LoadingDialog mLoadingDialog;
    protected Activity mActivity;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
    }



    public Resources safetyGetResources() {
        try {
            Context context = getContext();
            if (context == null) {
                return TGApplicationBase.getApplication().getResources();
            } else {
                return getResources();
            }
        } catch (Throwable ex) {
            return TGApplicationBase.getApplication().getResources();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    protected void showLoading(){
        if (mLoadingDialog==null){
            mLoadingDialog=new LoadingDialog(mActivity);
            mLoadingDialog.setMsg(R.string.loading);
        }
        mLoadingDialog.show();
    }

    protected void showLoading(String message){
        if (mLoadingDialog==null){
            mLoadingDialog=new LoadingDialog(mActivity);
            mLoadingDialog.setMsg(message);
        }
        mLoadingDialog.show();
    }

    protected void hideLoading(){
        if (mLoadingDialog!=null&&mLoadingDialog.isShowing()){
            mLoadingDialog.cancel();
        }
    }

    public int getScreenWidth() {
        WindowManager wm = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;// 屏幕宽度(dp)
    }

    public int getScreenHeight() {
        WindowManager wm = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;// 屏幕宽度(dp)
    }

}
